package com.dongdongshop.controller;

import com.dongdongshop.common.AjaxResult;
import com.dongdongshop.common.ResultEnum;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.service.ContentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("content")
public class ContentController {
    Logger logger = LoggerFactory.getLogger(ContentController.class);
    @DubboReference
    private ContentService contentService;
    /*广告管理*/
    /*跳转广告管理*/
    @RequestMapping("getContentHtml")
    public String getContentHtml(){
        return "/admin/content";
    }
    /*查询全部广告*/
    @RequestMapping("getContentAll")
    @ResponseBody
    public List<TbContent> getContentAll(){
        return contentService.getContentAll();
    }
    /*添加广告*/
    @RequestMapping("addContent")
    @ResponseBody
    public AjaxResult addContent(TbContent tbContent){
        try {
            int i  = contentService.addContent(tbContent);
            if (i>0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("添加广告失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
    /*修改广告*/
    /*1:回显*/
    @RequestMapping("updateContentEcho")
    @ResponseBody
    public AjaxResult updateContentEcho(Long id){
        try {
            TbContent tbContent  = contentService.updateContentEcho(id);
            if (tbContent != null){
                return AjaxResult.R(ResultEnum.SUCCESS).setData(tbContent);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("添加广告失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
    /*2:执行修改*/
    @RequestMapping("updateContent")
    @ResponseBody
    public AjaxResult updateContent(TbContent tbContent){
        try {
            int i  = contentService.updateContent(tbContent);
            if (i>0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("添加广告失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
    /*删除广告*/
    @RequestMapping("deleteContent")
    @ResponseBody
    public AjaxResult deleteContent(Long id){
        try {
            int i  = contentService.deleteContent(id);
            if (i>0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("添加广告失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
    /*
    * confirm("你确认要开启吗？")
      confirm("你确认要屏蔽吗？")
    * */
    @RequestMapping("onOperationContent")
    @ResponseBody
    public AjaxResult onOperationContent(@RequestParam("ids[]") Long[] ids){
        try {
            int i  = contentService.updateOnOperationContent(ids);
            if (i>0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("开启广告!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
    @RequestMapping("closeOperationContent")
    @ResponseBody
    public AjaxResult updateCloseOperationContent(@RequestParam("ids[]") Long[] ids){
        try {
            int i  = contentService.updateCloseOperationContent(ids);
            if (i>0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("开启广告!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
}
